// pages/pavilion/pavilion.js
var app = getApp;
const router = require('../../config').router

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lastPage:0,//最后一页
    pageNo:1,
    num: 0,
    titlenum: 0,
    headerlist: [{
        icon: 'dt',
        title: '展会动态'
      },
      {
        icon: 'book',
        title: '展馆介绍'
      }
    ],
    listname: [{
        name: '历史展出'
      },
      {
        name: '即将展出'
      },
      {
        name: '正在展出'
      }
    ],
    list: [
      // {
      //   beginDateStr:"2018-02-02",
      //   createDateStr:"2018-01-26 14:59:38",
      //   description:"",
      //   endDateStr:"2018-02-04",
      //   id:800,
      //   image:"http://www.jsxcpd.com/ftpdata/userfile/survey/imges/20180126/20180126152518765.jpg",
      //   title:"第四届江苏优质农产品（南京）展销会",
      //   video:""
      // }
    ],
    article: {
      // address: "江苏省南京市建邺区乐山路190号",
      // buildDesc:"江苏国际农业展览中心位于南京市建邺区乐山路190号，总建筑面积15000平方米，其中主体展馆面积共8000平方米，可设300个标准展位。农展中心具有鲜明独特的建筑特色，主要开拓中小型展览会，定位于“专业、特色、精品”展，可举办各种形式的展示展销会、推介会、产品发布会、招商会等。中心将建立顾客导向的经营理念，培养以提供顾客满意为主的经营意识，以优质高效服务取胜，为展会提供舒适安全环境，保证会展活动有序进行。",
      // buildId:1,
      // lat:"32.009282",
      // lon:"118.720281",
      // name:"江苏国际农业展览中心",
      // phone:"025-86263188",
      // photo:"http://www.jsxcpd.com/ftpdata/userfile/survey/imges/20161209/20161209183321816.png",
      // title:"江苏省农业委员会",
      // trailer:"http://www.jsxcpd.com/ftpdata/userfile/survery/video/20170113/20170113130911372.mp4",
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    //农展馆信息
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'query.exhibition.buildInfo',
        v: '1.0',
        format: 'json'
      },
      success: function(res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data)
          that.setData({
            article: res.data,
          })
        }
      }
    });
    //获取历史展出列表
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'query.exhibition.previous',
        v: '1.0',
        format: 'json',
        pageNo: 1
      },
      success: function(res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data)
          // console.log(res.data.page.last)
          // console.log(res.data.page.pageNo)
          that.setData({
            list: res.data.article,
            lastPage: res.data.page.last,
            pageNo: res.data.page.pageNo,
          })
        }
      }
    });
  },

  /**
   * banner视频点击
   */
  bannerClick: function(e) {
    var url = e.currentTarget.dataset.url;
    var id = parseInt(new Date().getTime() / 1000);
    wx.setStorageSync(id.toString(), url);
    // console.log(this.data.article)
    // console.log(id)
    if (url) {
      wx.navigateTo({
        url: '/pages/home/link/link?id=' + id
      })
    }
  },

  /**
   * 拨打电话
   */
  clickPhone: function(e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },

  /**
   * 获取定位
   */
  clickAddress: function(e) {
    var that = this;
    var storeName = e.currentTarget.dataset.name;
    var lon = parseInt(that.data.article.lon);
    var lat = parseInt(that.data.article.lat);
    wx.openLocation({
      latitude: lat || 0,
      longitude: lon || 0,
      name: storeName || '',
      address: storeName || ''
    })
  },

  /**
   * 展会动态下tab切换
   */
  change: function(e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    var method = '';
    that.setData({
      num: idx
    })
    var method = '';
    if (idx == 0) {
      method = 'query.exhibition.previous'
    } else if (idx == 1) {
      method = 'query.exhibition.open'
    } else {
      method = 'query.exhibition.progress'
    }
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: method,
        v: '1.0',
        format: 'json'
      },
      success: function(res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data)
          if (res.data.article == undefined) {
            that.setData({
              list: []
            })
          } else {
            that.setData({
              list: res.data.article
            })
          }
        }
      }
    });
  },

  /**
   * 展会tab切换
   */
  changetitle(e) {
    this.setData({
      titlenum: e.currentTarget.dataset.idx
    })
  },
  //文章详情
  articleClick: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/index?id=' + id
    })
  },
  //触底加载
  reload() {
    var that = this
    var p = that.data.pageNo
    p++
    var last = that.data.lastPage
    if(p<=last){
      // console.log(p)
      wx.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 1000,
        success: function () {
          wx.request({
            url: router,
            data: {
              appKey: 'w4q897jgvxkb',
              method: 'query.exhibition.previous',
              v: '1.0',
              format: 'json',
              pageNo: p
            },
            success: function (res) {
              if (res.statusCode == 200 && res.data.error != 'error') {
                // console.log(res.data)
                for (var i = 0; i < res.data.article.length; i++) {
                  that.data.list.push(res.data.article[i])
                }
                setTimeout(function(){
                  that.setData({
                    list: that.data.list,
                    pageNo: res.data.page.pageNo
                  })
                },1000)
              }
            }
          });
        }
      })
    }else{
      wx.showToast({
        title: '没有更多了',
        icon:'loading',
        duration: 1500
      })
    }
  }
})